package com.sanjiahantang.youjiarole.module.me.quotation.models.impl;

import android.content.Context;
import android.util.Log;

import com.google.gson.Gson;
import com.sanjiahantang.youjiarole.base.BaseBean;
import com.sanjiahantang.youjiarole.base.CallBack;
import com.sanjiahantang.youjiarole.config.ConfigValue;
import com.sanjiahantang.youjiarole.config.StaticValue;
import com.sanjiahantang.youjiarole.http.BaseDelegate;
import com.sanjiahantang.youjiarole.http.OkHttpClientManager;
import com.sanjiahantang.youjiarole.module.common.beans.StringBean;
import com.sanjiahantang.youjiarole.module.me.quotation.beans.ContractSignBean;
import com.sanjiahantang.youjiarole.module.me.quotation.beans.QuotationResponseBean;
import com.sanjiahantang.youjiarole.module.me.quotation.models.IQuotationModel;
import com.sanjiahantang.youjiarole.module.me.require.beans.RequireDetailInitResponseBean;
import com.squareup.okhttp.Request;

import java.util.Map;

/**
 * Created by zy on 2017/10/31.
 */

public class QuotationModel implements IQuotationModel,StaticValue {

    @Override
    public void searchGoodsDesign(final Context context, final Map<String, String> param, final CallBack<QuotationResponseBean> callBack) {
        OkHttpClientManager.postAsyn(context, ConfigValue.API_URL + "/mall/goods/phone/searchGoodsDesign",
                param,new BaseDelegate.ResultCallback<QuotationResponseBean>() {

                    @Override
                    public void onError(Request request, Object tag, Exception e) {
                        callBack.onFailure(e.getMessage());
                    }

                    @Override
                    public void onResponse(QuotationResponseBean response, Object tag) {
                        if (response != null && response.getCode() == RESPONSE_CODE_SUC){
                            loadDefaultGoodsDesign(context,param,response,callBack);
                        }else{
                            callBack.onSuccess(response,context,tag);
                        }
                    }
                });
    }

    private void loadDefaultGoodsDesign(final Context context, Map<String, String> param, final QuotationResponseBean quotationResponseBean, final CallBack<QuotationResponseBean> callBack){
        OkHttpClientManager.postAsyn(context, ConfigValue.API_URL + "/project/role/plan/searchPriceDesignPhoneForRole",
                param,new BaseDelegate.ResultCallback<StringBean>() {

                    @Override
                    public void onError(Request request, Object tag, Exception e) {
                        callBack.onFailure(e.getMessage());
                    }

                    @Override
                    public void onResponse(StringBean response, Object tag) {
                        if (response != null && response.getCode() == RESPONSE_CODE_SUC){
                            if (!"null".equals(response.getData())){
                                QuotationResponseBean.Plan plan = QuotationResponseBean.Plan.objectFromData(response.getData());
                                if (plan.getPspId() > 0){
                                    quotationResponseBean.setPspId(plan.getPspId());
                                }


                            }
                        }
                        callBack.onSuccess(quotationResponseBean,context,tag);
                    }
                });
    }

    @Override
    public void saveProjectServicePriceDesignPhone(final Context context, Object param, final CallBack<BaseBean> callBack) {
        OkHttpClientManager.postJson(context, ConfigValue.API_URL + "/project/role/plan/saveProjectServicePriceDesignPhone",
                param,new BaseDelegate.ResultCallback<BaseBean>() {

                    @Override
                    public void onError(Request request, Object tag, Exception e) {
                        callBack.onFailure(e.getMessage());
                    }

                    @Override
                    public void onResponse(BaseBean response, Object tag) {
                        callBack.onSuccess(response,context, tag);
                    }
                });
    }

    @Override
    public void contractSignForDesign(final Context context, final Map<String, Object> param, final Map<String, Object> param2, final CallBack<BaseBean> callBack) {
        OkHttpClientManager.postJson(context, ConfigValue.API_URL + "/project/contract/phone/contractSignForDesign",
                param,new BaseDelegate.ResultCallback<ContractSignBean>() {

                    @Override
                    public void onError(Request request, Object tag, Exception e) {
                        callBack.onFailure(e.getMessage());
                    }

                    @Override
                    public void onResponse(ContractSignBean response, Object tag) {
                        final BaseBean bean = new BaseBean();

                        if (response != null){

                            param2.put("contractCode",response.getData().getContractCode());
                            param2.put("signer1",response.getData().getSigner1());
                            param2.put("pcId",response.getData().getPcId());
                            createTemplatePdfForm(context, param2, new CallBack<BaseBean>() {
                                @Override
                                public void onSuccess(BaseBean response, Context context, Object tag) {
                                    callBack.onSuccess(response,context, param2);
                                }

                                @Override
                                public void onFailure(String message) {
                                    bean.setCode(RESPONSE_CODE_FAIL);
                                    bean.setMessage(message);
                                    callBack.onSuccess(bean,context, null);
                                }
                            });
                        }else{
                            bean.setCode(RESPONSE_CODE_FAIL);
                            bean.setMessage(response.getMessage());
                            callBack.onSuccess(bean,context, tag);
                        }

                    }
                });
    }

    private void createTemplatePdfForm(final Context context, Map<String, Object> param, final CallBack<BaseBean> callBack){
        OkHttpClientManager.postJson(context, ConfigValue.API_URL + "/sign/createTemplatePdfForm",
                param,new BaseDelegate.ResultCallback<BaseBean>() {

                    @Override
                    public void onError(Request request, Object tag, Exception e) {
                        callBack.onFailure(e.getMessage());
                    }

                    @Override
                    public void onResponse(BaseBean response, Object tag) {
                        callBack.onSuccess(response,context,tag);

                    }
                });
    }
}
